import urllib2
from osgeo import gdal, gdalconst
from time import time
from numpy import * 

def se(conf,inputs,outputs):
    rain_factor=inputs["rain_factor"]["value"]
    r2=inputs["r2"]["href"]

    data=urllib2.urlopen(r2)
    filename='/tmp/r2_'+str(time())+'.tiff'
    try:
        f=open(filename,'wb')
        f.write(data.read())
        f.close()
        msg="file created"
    except:
        msg="error in file creation"

    gdal.AllRegister()
    driver=gdal.GetDriverByName('GTiff')
    ds=gdal.Open(filename)
    if ds is None:
        msg=msg+'could not open gtiff'

    bands = ds.RasterCount
    cols=ds.RasterXSize
    rows=ds.RasterYSize

    r2arr=ds.GetRasterBand(1).ReadAsArray(0,0,cols, rows)
    r2rainfactor=dot(rain_factor,r2arr)
    before=r2arr[10,10]
    #after=r2rainfactor[10,10]
        
    outputs["Result"]["value"]=str(rain_factor)+str(msg)+str(before)
    return 3
